平衡二叉树 Posted on 2019-10-13 | In 剑指offer | | reads times 平衡二叉树题目描述 输入一棵二叉树,判断该二叉树是否是平衡二叉树。 平衡二叉树是左右深度差不超过1的二叉树,所以在遍历深度的同时需要判断是否深度差超过1。 12345678910111213function IsBalanced_Solution(pRoot){ // write code here return TreeDepth(pRoot)!==-1;}function TreeDepth(pRoot){ if(pRoot==null)return 0; const leftdep=TreeDepth(pRoot.left); if(leftdep==-1)return -1; const rightdep=TreeDepth(pRoot.right); if(rightdep==-1)return -1; return Math.abs(leftdep-rightdep)>1? -1 : Math.max(leftdep,rightdep)+1} Post author: GoldMiner Xun Post link: https://goldminerxun.github.io/2019/10/13/%E5%89%91%E6%8C%87offer%20JavaScript%E7%89%88%20(39)/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.